model{
  for(i in 1:n.obs){  #likelihood
      d.credit[i] ~ dnorm(mu[i], tau.y[i])
      mu[i] <- a.unit[country[i]] + b.l.credit[country[i]]*lag.credit[i] + XB[i]
      XB[i] <- b.l.top1*lag.ineq[i] + b.d.top1*d.ineq[i]+   
        b.cl*cum.left[i] +
        b.l.unemp*lag.unemp[i] + b.d.unemp*d.unemp[i] +
        b.l.pop*lag.pop[i] + b.d.pop*d.pop[i] +
        b.l.gdp*lag.gdp[i] + b.d.gdp*d.gdp[i] + #b.d.growth*d.growth[i] +
        b.l.k*lag.gfcf[i] + b.d.k*d.gfcf[i] +
        b.l.cab*lag.cab[i] + b.d.cab*d.cab[i] + 
        b.l.budg*lag.budg[i] + b.d.budg*d.budg[i] +
        b.l.world.save*lag.world.save[i] + b.d.world.save*d.world.save[i] + 
        b.l.bm*lag.bm.growth[i] + b.d.bm*d.bm.growth[i] +
        b.l.old*lag.old[i] + b.d.old*d.old[i]    
    lag.credit[i] ~ dnorm(mu.l.credit, tau.l.credit)#imputation models for missing covariates
    lag.ineq[i] ~ dnorm(mu.l.top1, tau.l.top1)
    d.ineq[i] ~ dnorm(mu.d.top1, tau.d.top1)
    lag.budg[i] ~ dnorm(mu.l.budg, tau.l.budg)
    d.budg[i] ~ dnorm(mu.d.budg, tau.d.budg)  
    lag.unemp[i] ~ dnorm(mu.l.unemp, tau.l.unemp)
    d.unemp[i] ~ dnorm(mu.d.unemp, tau.d.unemp)
    lag.bm.growth[i] ~ dnorm(mu.l.bm.growth, tau.l.bm.growth)
    d.bm.growth[i] ~ dnorm(mu.d.bm.growth, tau.d.bm.growth)
    logsigma2.y[i] <- g.unit[country[i]] + g.year[years.d[i]] + g.euro*eurozone[i] #model for the variance
    tau.y[i] <- 1/exp(logsigma2.y[i])
    res[i]<-d.credit[i] - mu[i] #residuals
    y.pred[i] ~ dnorm(mu[i], tau.y[i]) #repredicting y  
  }
  
  for(j in 1:(n.countries)){#country RE in model for variance  
    g.unit[j] ~ dnorm(0, tau.vc)
    a.unit[j] ~ dnorm(mu.c, tau.country) #country RE
    b.l.credit[j] <- -1*temp[j]
    temp[j] ~ dbeta(1,1)
  }
  for(j in 1:n.years){#country RE in model for variance
    g.year[j] ~ dnorm(0, tau.vy) 
  }
  
# priors
  mu.c ~ dnorm(0,0.00001) #country RE
  ##imputation means
  mu.l.credit ~ dnorm(0,0.00001) 
  mu.l.top1 ~ dnorm(0,0.00001)
  mu.d.top1 ~ dnorm(0,0.00001)
  mu.l.budg ~ dnorm(0,0.00001)
  mu.d.budg ~ dnorm(0,0.00001)  
  mu.l.unemp ~ dnorm(0,0.00001)
  mu.d.unemp ~ dnorm(0,0.00001)
  mu.l.bm.growth ~ dnorm(0,0.00001)
  mu.d.bm.growth ~ dnorm(0,0.00001)
  ## regression parameters
  b.l.top1~ dnorm(0,0.0001)
  b.d.top1~ dnorm(0,0.0001)
  b.cl~ dnorm(0,0.0001)
  b.l.unemp~ dnorm(0,0.0001)
  b.d.unemp~ dnorm(0,0.0001)
  b.l.pop~ dnorm(0,0.0001)
  b.d.pop~ dnorm(0,0.0001)
  b.l.gdp~ dnorm(0,0.0001)
  b.d.gdp~ dnorm(0,0.0001)
  b.l.k~ dnorm(0,0.0001)
  b.d.k~ dnorm(0,0.0001)
  b.l.cab~ dnorm(0,0.0001)
  b.d.cab~ dnorm(0,0.0001)
  b.l.budg~ dnorm(0,0.0001)
  b.d.budg~ dnorm(0,0.0001)
  b.l.world.save~ dnorm(0,0.0001)
  b.d.world.save~ dnorm(0,0.0001)
  b.l.bm~ dnorm(0,0.0001)
  b.d.bm~ dnorm(0,0.0001)
  b.l.old~ dnorm(0,0.0001)
  b.d.old~ dnorm(0,0.0001)
  g.euro ~dnorm(0,0.0001)
  ## variances/precisions
  sigma.country ~ dunif(0,50)
  tau.country <- pow(sigma.country,-2)
  sigma.vc ~ dunif(0,10) #variance for country RE in variance term
  sigma.vy ~ dunif(0,10) #variance for year RE in variance term
  sigma.l.credit ~ dunif(0,50)
  tau.l.credit<- pow(sigma.l.credit,-2)
  sigma.l.top1 ~ dunif(0,50)
  sigma.d.top1~ dunif(0,50)
  sigma.l.budg~ dunif(0,50)
  sigma.d.budg~ dunif(0,50)
  sigma.l.unemp~ dunif(0,50)
  sigma.d.unemp~ dunif(0,50)
  sigma.l.bm.growth~ dunif(0,50)
  sigma.d.bm.growth~ dunif(0,50)
  tau.vc <- pow(sigma.vc, -2)
  tau.vy <- pow(sigma.vy, -2)
  tau.l.top1 <- pow(sigma.l.top1,-2)
  tau.d.top1<- pow(sigma.d.top1,-2)
  tau.l.budg<- pow(sigma.l.budg,-2)
  tau.d.budg<- pow(sigma.d.budg,-2)
  tau.l.unemp<- pow(sigma.l.unemp,-2)
  tau.d.unemp<- pow(sigma.d.unemp,-2)
  tau.l.bm.growth<- pow(sigma.l.bm.growth,-2)
  tau.d.bm.growth<- pow(sigma.d.bm.growth,-2)
}
